<?php
namespace app\common\model;


class Jfdh extends Base
{
    public static function search($id = WEB_ID)
    {
        $page = input('post.page/d');
        $pageSize = input('post.pageSize/d');
        $page = ($page < 1) ? 1 : $page;
        $pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
        $self = new static();
        $query = $self->alias('a');
        is_numeric($id) && $query->where('a.zid', '=', $id);
        $query2 = clone $query;
        $list = $query->page($page, $pageSize)->order('a.time desc')->select();
        return [
            'total' => $query2->count('a.id'),
            'list' => $list
        ];
    }
	
    public static function add($data)
    {
        $self = new static();
        if ($result = $self->insert($data)) {
            return $result;
        } else {
            return false;
        }
    }

    public static function jfdh($userInfo, $data)
    {
        if ($userInfo['jf'] >= $data['jf']) {
            switch ($data['type']) {
                case 'vip':
                    //开通时间
                    $vip_start = date("Y-m-d");
                    //计算开通的vip时长
                    if ($userInfo['viptime']) {
                        $vip_time = date("Y-m-d", strtotime("+" . $data['shop'] . " day", strtotime($userInfo['viptime'])));
                    } else {
                        $vip_time = date("Y-m-d", strtotime("+" . $data['shop'] . " day"));
                    }
                    $upUser = Users::updateByUid($userInfo['uid'], [
                        'jf' => $userInfo['jf'] - $data['jf'],
                        'vipstart' => $vip_start,
                        'viptime' => $vip_time
                    ]);
                    if ($upUser) {
                        self::add([
                            'uid' => $userInfo['uid'],
                            'nickname' => $userInfo['nickname'],
							'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
							'jf' => $data['jf'],
                            'value' => 'VIP会员' . $data['shop'] . '天',
                            'time' => date('y-m-d H:i:s'),
                            'zid' => WEB_ID
                        ]);
                        $result = [
                            'code' => 0,
                            'message' => '消耗' . $data['jf'] . '点积分兑换VIP会员' . $data['shop'] . '天成功！'
                        ];
                    }
                    break;
                case 'peie':
                    $upUser = Users::updateByUid($userInfo['uid'], [
                        'jf' => $userInfo['jf'] - $data['jf'],
                        'peie' => $userInfo['peie'] + $data['shop'],
                    ]);
                    if ($upUser) {
                        self::add([
                            'uid' => $userInfo['uid'],
                            'nickname' => $userInfo['nickname'],
							'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
							'jf' => $data['jf'],
                            'value' => '配额' . $data['shop'] . '个',
                            'time' => date('y-m-d H:i:s'),
                            'zid' => WEB_ID
                        ]);
                        $result = [
                            'code' => 0,
                            'message' => '消耗' . $data['jf'] . '点积分兑换' . $data['shop'] . '个配额成功！'
                        ];
                    }
                    break;
                case 'daili':
                    //代理等级
                    $dj = $data['shop'];
                    //开通时间
                    $vip_start = date("Y-m-d");
                    //计算开通的vip时长
                    if ($userInfo['viptime']) {
                        $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $data['shop'] . '') . " day", strtotime($userInfo['viptime'])));
                    } else {
                        $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $data['shop'] . '') . " day"));
                    }
                    //修改用户信息
                    $upUser = Users::updateByUid($userInfo['uid'], [
                        'dlpower' => $dj,
                        'vipstart' => $vip_start,
                        'viptime' => $vip_time,
						'jf' => $userInfo['jf']-$data['jf'],
                        'money' => $userInfo['money'] + config('sys_dl_give_rmb_' . $dj . ''),
                        'dlzk' => config('sys_dl_give_z_' . $dj . '')
                    ]);
                    if ($upUser) {
                        self::add([
                            'uid' => $userInfo['uid'],
                            'nickname' => $userInfo['nickname'],
							'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
							'jf' => $data['jf'],
                            'value' => is_daili($data['shop']),
                            'time' => date('y-m-d H:i:s'),
                            'zid' => WEB_ID
                        ]);
                        $result = [
                            'code' => 0,
                            'message' => '消耗' . $data['jf'] . '点积分兑换' . is_daili($data['shop']) . '成功！'
                        ];
                    }
                    break;
            }
        } else {
            $result = [
                'code' => 1,
                'message' => '兑换失败，您的积分不足'
            ];
        }
        return $result;
    }
}